import { cloneDeep } from 'lodash-es'

const pcaObject = require('./pca.json')

const regionData = []

// 计算省
for (const prop in pcaObject) {
  regionData.push({
    value: prop,
    label: prop
  })
}
// 计算市
for (let i = 0, len = regionData.length; i < len; i++) {
  const province = regionData[i].value
  const provinceChildren = []
  for (const prop in pcaObject[province]) {
    provinceChildren.push({
      value: prop,
      label: prop
    })
  }
  if (provinceChildren.length) {
    regionData[i].children = provinceChildren
  }
}

// 计算区
for (let i = 0, len = regionData.length; i < len; i++) {
  const province = regionData[i].value
  const provinceCity = regionData[i].children
  for (let j = 0, len = provinceCity.length; j < len; j++) {
    const city = provinceCity[j].value
    const cityChildren = []
    for (const prop of pcaObject[province][city]) {
      cityChildren.push({
        value: prop,
        label: prop
      })
    }
    if (cityChildren.length) {
      provinceCity[j].children = cityChildren
    }
  }
}

const regionDataAreaPlus = cloneDeep(regionData)
for (let i = 0, len = regionDataAreaPlus.length; i < len; i++) {
  const province = regionDataAreaPlus[i].children
  if (province && province.length) {
    for (let j = 0, len = province.length; j < len; j++) {
      const city = province[j].children
      if (city && city.length) {
        city.unshift({
          value: '所有区县',
          label: '所有区县'
        })
      }
    }
  }
}

const regionDataAreaPro = cloneDeep(regionData)
for (let i = 0, len = regionDataAreaPro.length; i < len; i++) {
  const province = regionDataAreaPro[i].children
  if (province && province.length) {
    for (let j = 0, len = province.length; j < len; j++) {
      if (province[j].value == '市辖区') {
        province[j].label = regionDataAreaPro[i].label
        province[j].value = regionDataAreaPro[i].value
      }
    }
  }
}

const regionDataAreaProPlus = cloneDeep(regionData)
for (let i = 0, len = regionDataAreaProPlus.length; i < len; i++) {
  const province = regionDataAreaProPlus[i].children
  if (province && province.length) {
    for (let j = 0, len = province.length; j < len; j++) {
      const city = province[j].children
      if (province[j].value == '市辖区') {
        province[j].label = regionDataAreaProPlus[i].label
        province[j].value = regionDataAreaProPlus[i].value
      }
      if (city && city.length) {
        city.unshift({
          value: '所有区县',
          label: '所有区县'
        })
      }
    }
  }
}
export { regionData, regionDataAreaPlus, regionDataAreaPro, regionDataAreaProPlus }